var url = "https://huitengit.com:9021";
layui.use(['form', 'layer', 'laydate', 'table'], function () {
	var form = layui.form,
		layer = layui.layer,
		$ = layui.jquery,
		laydate = layui.laydate,
		table = layui.table;
	//新闻列表
	option = {
		elem: '#newsList',
		id: 'newsList1',
		url: url + '/dutyroster/querydutyroster',
		page: true,
		// height: "full-140",
		limit: 7,
		where: {
			keyWord: ''
		},
		text: {
			none: '暂无相关数据' //默认：无数据
		},
		request: {
			pageName: "currentPage", //页码的参数名称，
			limitName: "size", //每页数据量的参数名，
		},
		headers: {
			token: localStorage.getItem('token')
		},
		parseData: function (res) { //res 即为原始返回的数据
			// console.log(res)
			return {
				"code": 0, //解析接口状态
				"msg": res.message, //解析提示文本
				"count": res.data.count, //解析数据长度
				"data": res.data.dutyrosterList //解析数据列表
			};

		},
		limits: [7, 9, 11, 13],
		cols: [
			[{
				type: "checkbox",
				fixed: "left"
			},
			{
				field: 'workingDate',
				title: '日期',
				align: 'center'
			},
			{
				field: 'caseDuty',
				title: '文件名',
				align: 'center'
			},
			// {
			// 	field: 'communityDuty',
			// 	title: '社区值班',
			// 	align: 'center'
			// },
			{
				title: '操作',
				templet: '#newsListBar',
				fixed: "right",
				align: "center"
			}
			]
		]
	};
	laydate.render({
		elem: '#changeDate'
	});
	var tableIns = table.render(option);

	// 添加值班表格内容
	form.on('submit(add)', function (data) {
		var data1 = {};
		data1.caseDuty = data.field.caseDuty;
		data1.communityDuty = data.field.communityDuty;
		data1.workingDate = data.field.workingDate;
		// console.log(data1)
		$.ajax({
			url: url + "/dutyroster/addorupdate",
			method: "post",
			data: data1,
			headers: {
				token: localStorage.getItem('token')
			},
			success: function (data) {
				// console.log(data)
				if (data.code === 1) {
					layer.closeAll();
					tableIns.reload();
					layer.msg("添加成功！")
				} else {
					layer.msg(data.msg)
				}
			}

		})
	});


	// 根据选中行删除选中内容
	$("#delete_Btn").on("click", function () {
		var checkStatus = table.checkStatus('newsList1'); //即为基础参数 id 对应的值
		var data1 = '';

		checkStatus.data.forEach((item) => {
			data1 += item.id + ',';
		});
		data1 = data1.substr(0, data1.length - 1)
		// console.log(data1)
		if (data1.length !== 0) {
			$.ajax({
				url: url + "/dutyroster/deletedutyroster",
				method: "post",
				data: {
					ids: data1
				},
				headers: {
					token: localStorage.getItem('token')
				},
				success: function (data) {
					// console.log(data)
					if (data.code === 1) {
						tableIns.reload();
						layer.msg('删除成功！')

					} else {
						layer.msg(data.message)
					}

				},
				error: function (err) {
					layer.msg('删除失败！')
				}
			})
		} else {
			layer.msg("请先选择需要删除的内容！", {
				icon: 2
			});
		}

	})

	//搜索功能
	$("#search").on("click", function () {
		// var data = form.val("search");
		var data = $("#searchInput").val();
		// console.log(data)
		option.where.keyWord = data
		table.reload("newsList1", option)
	})

	//列表操作：编辑与删除
	table.on('tool(newsList1)', function (obj) {
		var layEvent = obj.event,
			data1 = obj.data;

		if (layEvent === 'edit') {
			// console.log(data1)
			// $("#changeDate").val(data)
			$("#editCaseDuty").val(data1.caseDuty);
			//	$("#editCommunityDuty").val(data1.communityDuty);
			$(".editChangeDate").val(data1.workingDate)
			layer.open({
				type: 1,
				title: '编辑',
				content: $('#editTableDuty'),
				area: ['421px', '260px'],
				btn: ['确认修改', '取消'],
				yes: function (index, layero) {
					var editDate = $("#editDate").val();
					var editCaseDuty = $("#editCaseDuty").val();
					//	var editCommunityDuty = $("#editCommunityDuty").val();
					var data2 = {}
					data2.workingDate = editDate;
					data2.caseDuty = editCaseDuty;
					//	data2.communityDuty = editCommunityDuty;
					data2.id = data1.id;
					// console.log(data2)
					$.ajax({
						url: url + "/dutyroster/addorupdate",
						method: "post",
						data: data2,
						headers: {
							token: localStorage.getItem('token')
						},
						success: function (data) {
							// console.log(data)
							if (data.code === 1) {
								layer.closeAll();
								tableIns.reload();
								layer.msg("修改成功！")
							} else {
								layer.msg(data.message)
							}
						}

					})
				}
			})
		} else {
			layer.confirm('确定删除此行内容？', {
				btn: ['确定', '取消']
			}, function () {
				// console.log("确定")
				console.log(data1);
				$.ajax({
					url: url + "/dutyroster/deletedutyroster?ids=" + data1.id,
					method: "post",
					headers: {
						token: localStorage.getItem('token')
					},
					success: function (data) {
						console.log(data)
						if (data.code === 1) {
							tableIns.reload();
							layer.msg('删除成功！')

						} else {
							layer.msg(data.message)
						}

					},
					error: function (err) {
						layer.msg('删除失败！')
					}
				})
			}, function () {
				console.log("取消")
			})
		}
	})


});
